package zjc.bookkeeping.tools;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import zjc.bookkeeping.R;

public class RingView extends View {

    private ValueAnimator valueAnimator;
    private int mViewCenterX;   //view宽的中心点(可以暂时理解为圆心)
    private int mViewCenterY;   //view高的中心点(可以暂时理解为圆心)

    private int mMinRadio; //最里面白色圆的半径
    private float mRingWidth; //圆环的宽度
    private int mMinCircleColor;    //最里面圆的颜色
    private int mRingNormalColor;    //默认圆环的颜色
    private int mRingColor;//    改变圆环的颜色
    private Paint mPaint;

    private RectF mRectF; //圆环的矩形区域
    private int mSelectRing = 0; //要显示的彩色区域(岁数值变化)
    private int mMaxValue;

    public RingView(Context context) {
        this(context,null);
    }

    public RingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RingView);
        //最里面白色圆的半径
        mMinRadio = typedArray.getInteger(R.styleable.RingView_min_circle_radio, 300);
        //圆环宽度
        mRingWidth = typedArray.getFloat(R.styleable.RingView_ring_width, 40);
        //最里面的圆的颜色(白色)
        mMinCircleColor = typedArray.getColor(R.styleable.RingView_circle_color, context.getResources().getColor(R.color.white));
        //圆环的默认颜色(圆环占据的是里面的圆的空间)
        mRingNormalColor = typedArray.getColor(R.styleable.RingView_ring_normal_color, context.getResources().getColor(R.color.blue_74A0CF));
        //圆环要显示的彩色的区域
        mRingColor = typedArray.getColor(R.styleable.RingView_ring_color,context.getResources().getColor(R.color.pink_FAE0D7));
        mSelectRing = typedArray.getInt(R.styleable.RingView_ring_color_select, 0);

        mMaxValue = typedArray.getInt(R.styleable.RingView_maxValue, 100);

        typedArray.recycle();
        //抗锯齿画笔
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //防止边缘锯齿
        mPaint.setAntiAlias(true);
        //需要重写onDraw就得调用此
        this.setWillNotDraw(false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        //view的宽和高,相对于父布局(用于确定圆心)
        int viewWidth = getMeasuredWidth();
        int viewHeight = getMeasuredHeight();
        mViewCenterX = viewWidth / 2;
        mViewCenterY = viewHeight / 2;
        //画矩形
        mRectF = new RectF(mViewCenterX - mMinRadio - mRingWidth / 2, mViewCenterY - mMinRadio - mRingWidth / 2, mViewCenterX + mMinRadio + mRingWidth / 2, mViewCenterY + mMinRadio + mRingWidth / 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(mMinCircleColor);
        canvas.drawCircle(mViewCenterX,mViewCenterY,mMinRadio,mPaint);
        drawNormalRing(canvas);
        drawColorRing(canvas);
    }

    private void drawColorRing(Canvas canvas) {
        Paint ringColorPaint = new Paint(mPaint);
        ringColorPaint.setStyle(Paint.Style.STROKE);
        ringColorPaint.setStrokeWidth(mRingWidth);
        ringColorPaint.setColor(mRingColor);
        canvas.rotate(-90,mViewCenterX,mViewCenterY);
        canvas.drawArc(mRectF,360,mSelectRing,false,ringColorPaint);
    }

    private void drawNormalRing(Canvas canvas) {
        Paint ringNormalPaint = new Paint(mPaint);
        ringNormalPaint.setStyle(Paint.Style.STROKE);
        ringNormalPaint.setStrokeWidth(mRingWidth);
        ringNormalPaint.setColor(mRingNormalColor);
        canvas.drawArc(mRectF,360,360,false,ringNormalPaint);
    }
    public void setValue(int nowValue,int afterValue){
        if (afterValue > mMaxValue){
            afterValue = mMaxValue;
        }
        int start = nowValue;
        int end = afterValue;
        startAnimation(start,end,2000);
    }

    private void startAnimation(int start, int end, int time) {
        valueAnimator = ValueAnimator.ofInt(start,end);
        valueAnimator.setDuration(time);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int i = Integer.valueOf(String.valueOf(valueAnimator.getAnimatedValue()));
                mSelectRing = (int) (360 * (i / 100f));
                invalidate();
            }
        });
        valueAnimator.start();
    }
}
